<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1300" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-col :span="8">
                <el-form-item label="装配日期" prop="entry_date">
                    <el-date-picker v-model="form.entry_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%" />
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="产品清单" prop="detail">
                    <el-button type="primary" size="small" icon="el-icon-plus" @click="checkTask">选择生产任务</el-button>
                    <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="350" hideAdd>
                        <el-table-column prop="goods_code" label="产品型号" align="center"></el-table-column>
                        <el-table-column prop="goods_name" label="速比" align="center"></el-table-column>
                        <el-table-column prop="goods_name" label="法兰" align="center"></el-table-column>
                        <el-table-column prop="goods_name" label="颜色" align="center"></el-table-column>
                        <el-table-column prop="goods_name" label="客户" align="center"></el-table-column>
                        <el-table-column label="数量" prop="number" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.number" placeholder="数量"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="备注1" prop="number" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.number" placeholder="备注1"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="备注2" prop="number" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.number" placeholder="备注2"></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <select-task v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></select-task>
</template>

<script>
import selectTask from '../task/selectTask.vue';
export default {
  components: { selectTask },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增装配',
				edit: '编辑装配',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id: "",
                entry_code: "",
                entry_date: this.moment().format('YYYY-MM-DD'),
                enclosure: "",
                remark: "",
                detail: [],
                plan_id: "",
                is_inverted: "",
                material_ware: "",
			},
            plan_code: "",
            ware_id: "",
			//验证规则
			rules: {
				plan_id: [{ required: true, message: '请选择销售' }],
			},
            dialog: {
                save: false,
            },
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.production.inhouse.edit.post(this.form);
					} else {
						res = await this.$API.production.inhouse.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.production.inhouse.detail.get({id:data.id});
            this.form.entry_code = res.data.entry_code
            this.form.plan_id = res.data.plan_id
            this.plan_code = res.data.plan.production_code
            this.form.entry_date = res.data.entry_date
            this.form.enclosure = res.data.enclosure
            this.form.remark = res.data.remark
            this.form.is_inverted = res.data.is_inverted
            this.form.material_ware = res.data.material_ware
            let array = []
            res.data.detail.map(item=>{
                array.push({
                    id:item.id,
                    goods_id:item.goods_id,
                    goods_code:item.goods && item.goods.goods_code,
                    goods_name:item.goods && item.goods.goods_name,
                    goods_spec:item.goods && item.goods.goods_spec,
                    goods_model:item.goods && item.goods.goods_model,
                    unit_name:item.goods && item.goods.unit.unit_name,
                    plan_number:item.plan_num.number,
                    can_number:item.plan_num.number - (item.plan_num.deliver_number + item.plan_num.enter_number),
                    number: item.number,
                    ware_id:item.ware_id
                })
            })
            this.form.detail = array
		},
        onFocus(event){
            event.target.select();
        },
        checkTask(){
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('add')
            })
        },
        handleSuccess(data){
            let array = []
            data.product.map(item=>{
                array.push({
                    goods_id:item.goods_id,
                    goods_name:item.goods.goods_name,
                    goods_spec:item.goods.goods_spec,
                    goods_model:item.goods.goods_model,
                    unit_name:item.goods.unit.unit_name,
                    goods_code:item.goods.goods_code,
                })
                this.form.detail = array
            })
        },
	}
}
</script>

<style></style>
